﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using SystemTypes;
using NetSettings;

namespace WindowsFormsApplication1
{
    public partial class SettingsForm : Form
    {
        public Settings _Settings;
        public FormMain _parent;
        public bool needUpdate = false;

        public delegate void UpdateSettings();
        public event UpdateSettings updateSettings;
        public void UpdateSet()
        {
            if (updateSettings != null) updateSettings();
        }

        public SettingsForm(Settings settings, FormMain parent)
        {
            InitializeComponent();
            _Settings = settings;
            _parent = parent;
            SetData();
            LoadData();
        }

        private void SetData()
        {
            List_arcs.Items.Add(Types.ArcType.IsA);
            List_arcs.Items.Add(Types.ArcType.APartOf);
            List_arcs.Items.Add(Types.ArcType.ElementOf);
            List_arcs.Items.Add(Types.ArcType.Precede);
            List_arcs.Items.Add(Types.ArcType.Use);
            List_arcs.Items.Add(Types.ArcType.Subtopic);

            //XmlSerializer xmlSerializer = new XmlSerializer(typeof(Settings));
            //_Settings.QuestionsInfo.QuestionsData[0].QuestionPattern = @"((что такое)|(что есть)|(чем является)) .+((с точки зрения) .+)?\??";
            //_Settings.QuestionsInfo.QuestionsData[0].AnswerPattern = @".+ это|(является частью) .+";
            //xmlSerializer.Serialize(new StreamWriter("settings.xml"), _Settings);
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            if(needUpdate)
            {
                var ans = MessageBox.Show(@"Применить все сохранённые изменения?", @"Сохранение изменений",
                                          MessageBoxButtons.YesNo);
                if (ans == DialogResult.Yes)
                {
                    var xmlSerializer = new XmlSerializer(typeof (Settings));
                    var writer = new StreamWriter("settings.xml");
                    xmlSerializer.Serialize(writer, _Settings);
                    writer.Close();
                }

                updateSettings += _parent.UpdateSettings;
                UpdateSet();
                updateSettings -= _parent.UpdateSettings;
            }
            Close();
        }

        private void LoadData()
        {
            foreach (var question in _Settings.QuestionsInfo.QuestionsData)
            {
                var type = question.QuestionType.ToString();
                cmbox_questionType.Items.Add(type);
            }
        }

        private void cmbox_questionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var currentQuestionType = cmbox_questionType.Text;
            var question = _Settings.QuestionsInfo.GetQuestionByTypeName(currentQuestionType);
            if(question == null)
            {
                MessageBox.Show(@"Тип вопроса не найден");
                return;
            }
            for (var i = 0; i < List_arcs.Items.Count; i++)
            {
                var listItem = List_arcs.Items[i];
                var check = question.SupportedArcs.Contains((Types.ArcType) listItem);
                List_arcs.SetItemChecked(i, check);
            }
            box_pattern.Text = question.QuestionPattern;
            box_ansPattern.Text = question.AnswerPattern;
        }

        private void cmbox_questionType_TextUpdate(object sender, EventArgs e)
        {
            cmbox_questionType.SelectedIndex = 0;
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (cmbox_questionType.Text.Trim() == "") return;
            var currentQuestionType = cmbox_questionType.Text;
            var question = _Settings.QuestionsInfo.GetQuestionByTypeName(currentQuestionType);
            question.SupportedArcs = new List<Types.ArcType>();
            for (var i = 0; i < List_arcs.Items.Count; i++)
            {
                var listItem = List_arcs.Items[i];
                if (List_arcs.GetItemChecked(i)) question.SupportedArcs.Add((Types.ArcType)listItem);
            }
            question.QuestionPattern = box_pattern.Text;
            question.AnswerPattern = box_ansPattern.Text;
            _Settings.QuestionsInfo.OverwriteQuestionSameType(question);

            needUpdate = true;
        }
    }
}
